package contact

import (
	"context"
	protos "goim-pro/api/protos/salty"
	. "goim-pro/examples/salty/helper"
	"goim-pro/pkg/logs"
	"goim-pro/pkg/utils"
)

var (
	UserId = "01EMK05C80JKJZF9MXGT8XS2KW" // 13631210008
	logger = logs.GetLogger("INFO")
)

func RequestContact(t protos.ContactServiceClient) {
	reqContactReq := &protos.RequestContactReq{
		TargetUserId: GetUserB().UserId,
		Reason:       "我是德玛西亚人，来交个朋友！",
	}
	anyData, _ := utils.MarshalMessageToAny(reqContactReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     anyData,
	}

	tr, err := t.RequestContact(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("login error: %s", err.Error())
	} else {
		var resp = &protos.RequestContactResp{}
		PrintResp(tr, resp)
	}
}

func AcceptContact(t protos.ContactServiceClient) {
	acpContactReq := &protos.AcceptContactReq{
		TargetUserId: GetUserA().UserId,
	}
	anyData, _ := utils.MarshalMessageToAny(acpContactReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserB().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserB().Token,
		Data:     anyData,
	}

	tr, err := t.AcceptContact(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("accept contact error: %s", err.Error())
	} else {
		var resp = &protos.AcceptContactResp{}
		PrintResp(tr, resp)
	}
}

func RefusedContact(t protos.ContactServiceClient) {
	refusedContactReq := &protos.RefusedContactReq{
		TargetUserId: GetUserA().UserId,
		Reason:       "我喜欢小姐姐",
	}
	anyData, _ := utils.MarshalMessageToAny(refusedContactReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserC().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserC().Token,
		Data:     anyData,
	}

	tr, err := t.RefusedContact(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("refused contact request error: %s", err.Error())
	} else {
		var resp = &protos.RefusedContactResp{}
		PrintResp(tr, resp)
	}
}

func UpdateRemarkInfo(t protos.ContactServiceClient) {
	updateRemarkInfoReq := &protos.UpdateContactRemarkReq{
		TargetUserId: GetUserB().UserId,
		RemarkInfo: &protos.ContactRemark{
			RemarkName:  "喜洋洋",
			Description: "He's a crazy boy.",
			Telephones:  []string{""},
			Tags:        []string{"Friend", "Boy"},
		},
	}
	anyData, _ := utils.MarshalMessageToAny(updateRemarkInfoReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     anyData,
	}

	tr, err := t.UpdateContactRemark(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("update remark info error: %s", err.Error())
	} else {
		var resp = &protos.UpdateContactRemarkResp{}
		PrintResp(tr, resp)
	}
}

func DeleteContact(t protos.ContactServiceClient) {
	delContactReq := &protos.DeleteContactReq{
		TargetUserId: GetUserB().UserId,
	}
	anyData, _ := utils.MarshalMessageToAny(delContactReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     anyData,
	}

	tr, err := t.DeleteContact(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("delete contact error: %s", err.Error())
	} else {
		var resp = &protos.DeleteContactResp{}
		PrintResp(tr, resp)
	}
}

func GetContactList(t protos.ContactServiceClient) {
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     nil,
	}

	tr, err := t.GetContacts(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("get contacts info error: %s", err.Error())
	} else {
		var resp = &protos.GetContactsResp{}
		PrintResp(tr, resp)
	}
}

func GetContactOperationList(t protos.ContactServiceClient) {
	getContactOperationRelationsReq := &protos.GetContactOperationRelationsReq{
		StartDateTime: -1,
		EndDateTime:   -1,
	}
	anyData, _ := utils.MarshalMessageToAny(getContactOperationRelationsReq)
	grpcReq := &protos.GrpcReq{
		UserId:   GetUserA().UserId,
		DeviceId: "LOCAL_DEV",
		Version:  "",
		Language: 0,
		Os:       0,
		Token:    GetUserA().Token,
		Data:     anyData,
	}

	tr, err := t.GetContactOperationRelations(context.Background(), grpcReq)
	if err != nil {
		logger.Errorf("get notification messages info error: %s", err.Error())
	} else {
		var resp = &protos.GetContactOperationRelationsResp{}
		PrintResp(tr, resp)
	}
}
